package com.ld.security.server.admin.modules.ge.vo;

import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.anno.SqlKey;
import com.ld.security.common.mybatis.constant.FieldTypeConstant;
import com.ld.security.common.mybatis.constant.SqlCondition;
import com.ld.security.server.admin.modules.ge.entity.GeColumnEntity;
import com.ld.security.server.admin.modules.ge.entity.GeFieldEntity;
import com.ld.security.server.admin.modules.ge.entity.GeTableEntity;
import com.ld.security.common.core.base.SuperVo;
import com.ld.security.common.mybatis.anno.QueryKey;
import com.ld.security.common.mybatis.anno.RemoveKey;
import com.ld.security.common.mybatis.anno.SaveKey;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class TableDetailsVo implements SuperVo {

    private String id;


    @SaveKey
    @QueryKey
    @RemoveKey
    @SqlKey(name = "id",valueField = "id")
    private GeTableEntity table;


    @RemoveKey
    @QueryKey
    @SqlKey(name = "tableId",valueField = "table.id")
    @SaveKey(name = "tableId",valueField = "table.id")
    private List<GeColumnEntity> columns;


    public List<GeFieldEntity> columnToFields(){
        List<GeFieldEntity> fs =  this.columns.stream().map(v -> {
            GeFieldEntity f =   new GeFieldEntity();
            f.setColumnName(v.getColumnName());
            f.setFieldName(StringUtil.columnToJava(v.getColumnName()));
            f.setLabel(v.getLabel());
            f.setTableName(v.getTableName());
            if(v.getColumnType().contains("var")){
                f.setFieldType(FieldTypeConstant.string);
                f.setSqlKey(SqlCondition.like);
            }else if(v.getColumnType().contains("text")){
                f.setFieldType(FieldTypeConstant.string);
            }else if(v.getColumnType().contains("date")){
                f.setFieldType(FieldTypeConstant.date);
                f.setSqlKey(SqlCondition.ge);
            }else if(v.getColumnType().contains("int")){
                f.setFieldType(FieldTypeConstant.number);
                f.setSqlKey(SqlCondition.ge);
            }
            ReflectionUtil.fill(f);

            return f;
        }).collect(Collectors.toList());
        return fs;
    }

}
